#include<iostream>
#include<unistd.h>
#include<signal.h>
#include<sys/wait.h>
#include<sys/types.h>

void notice(int signo)
{
    std::cout<<"I am father prcess, get a "<<signo<< " signo, pid: "<<getpid()<<std::endl;
    pid_t rid = waitpid(-1,nullptr,0);
    if(rid>0)                        //等待子进程
    {
        std::cout<<"wait child success, rid: "<<rid<<std::endl;
    }
}

void DoOtherThing()
{
    std::cout<<" DoOtherThing()~"<<std::endl;
}

int main()
{
    signal(SIGCHLD,notice);
    pid_t id = fork();               //创建子进程
    if(id==0)
    {
        std::cout<<"I am child process, pid: "<<getpid()<<std::endl;
        sleep(3);
        exit(1);
    }

    //父进程
    while(true)
    {
        DoOtherThing();                //父进程做其他事情，子进程退出后，会给父进程发17号信号 
        sleep(1);    
    }
    
}